package com.mcdroid.turret;

import android.graphics.Bitmap;
import android.graphics.Color;

public class MovementDetector {
	private Byte[] mPreviousDataYUV;
	private Byte[] mPreviewImageDataYUV; // preview image data in YUV420 format

	private Bitmap mDifferenceImage;
	//TODO: remove
	public  Bitmap mImage;
	int mImageWidth, mImageHeight;

	public Bitmap getDifferenceImage() {
		return mDifferenceImage;
	}

	public MovementDetector(byte[] previousData, byte[] data, int imageWidth, int imageHeight) {
		mImageWidth = imageWidth;
		mImageHeight = imageHeight;
		// CalculateDifferenceImage(previousData, data);
	}

	public MovementDetector(Bitmap previousImage, byte[] data, int imageWidth, int imageHeight) {
		mImageWidth = imageWidth;
		mImageHeight = imageHeight;
		mImage = decodeYUV420SP(data, imageWidth, imageHeight);
		mDifferenceImage = CalculateDifferenceImage(previousImage, mImage);
	}

	// public MovementDetector(byte[] previousData, byte[] data, int imageWidth,
	// int imageHeight) {}
	// private Bitmap CalculateDifferenceImage(Byte[] previousImage, Byte[]
	// image)

	private static Bitmap CalculateDifferenceImage(Bitmap previousImage, Bitmap image) {
		Bitmap bitmap = image.copy(image.getConfig(), true);
		int imageWidth = bitmap.getWidth();
		int imageHeight = bitmap.getHeight();
		if (previousImage != null) {
			int previousPixel, currentPixel;
			int previousRed, currentRed;
			int previousBlue, currentBlue;
			int previousGreen, currentGreen;
			int diffRed, diffGreen, diffBlue;
			for (int i = 0; i < imageWidth; i++) {
				for (int j = 0; j < imageHeight; j++) {
					previousPixel = previousImage.getPixel(i, j);
					currentPixel = bitmap.getPixel(i, j);
					previousRed = 0x00ff0000 & previousPixel;
					currentRed = 0x00ff0000 & currentPixel;
					previousGreen = 0x0000ff00 & previousPixel;
					currentGreen = 0x0000ff00 & currentPixel;
					previousBlue = 0x000000ff & previousPixel;
					currentBlue = 0x000000ff & currentPixel;
					diffRed = Math.abs(currentRed - previousRed);
					diffGreen = Math.abs(currentGreen - previousGreen);
					diffBlue = Math.abs(currentBlue - previousBlue);
					bitmap.setPixel(i, j, Color.rgb(diffRed, diffGreen, diffBlue));
				}
			}
		}
		return bitmap;

	}

	static public Bitmap decodeYUV420SP(byte[] yuv420sp, int width, int height) {
		final int frameSize = width * height;
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		for (int j = 0, yp = 0; j < height; j++) {
			int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
			for (int i = 0; i < width; i++, yp++) {
				int y = (0xff & ((int) yuv420sp[yp])) - 16;
				if (y < 0)
					y = 0;
				if ((i & 1) == 0) {
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}

				int y1192 = 1192 * y;
				int r = (y1192 + 1634 * v);
				int g = (y1192 - 833 * v - 400 * u);
				int b = (y1192 + 2066 * u);

				if (r < 0)
					r = 0;
				else if (r > 262143)
					r = 262143;
				if (g < 0)
					g = 0;
				else if (g > 262143)
					g = 262143;
				if (b < 0)
					b = 0;
				else if (b > 262143)
					b = 262143;
				int red = ((r << 6) & 0xff0000) >> 16;
				int green = ((g >> 2) & 0xff00) >> 8;
				int blue = ((b >> 10) & 0xff);
				bitmap.setPixel(i, j, Color.rgb(red, green, blue));
				// rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
				// 0xff00) | ((b >> 10) & 0xff);
			}
		}
		return bitmap;
	}

}
